package com.xn.app.muying.util;

import android.app.Service;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;

/**
 * 手机震动
 * Created by gaoge on 2017/2/14.
 */

public class VibratorUtil {
    /**
     * final Activity activity  ：调用该方法的Activity实例
     * long milliseconds ：震动的时长，单位是毫秒
     * long[] pattern  ：自定义震动模式 。数组中数字的含义依次是[静止时长，震动时长，静止时长，震动时长。。。]时长的单位是毫秒
     * boolean isRepeat ： 是否反复震动，如果是true，反复震动，如果是false，只震动一次
     */
    static Vibrator vib = null;
    static MediaPlayer mMediaPlayer = null;

    public static void Vibrate(final Context context, boolean isRepeat) {
        long[] l = {500, 500, 500, 500};
        vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(l, 2);
    }

    //关闭震动
    public static void closeVib() {
        vib.cancel();
    }

    //播放铃声
    public  void startAlarm(final Context context) {
        try {
            mMediaPlayer = MediaPlayer.create(context, getSystemDefultRingtoneUri(context));
            mMediaPlayer.setLooping(true);//设置循环


            mMediaPlayer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //关闭铃声
    public static void closeMediaPlayer() {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        mMediaPlayer = null;
    }

    //获取系统默认铃声的Uri
    private  Uri getSystemDefultRingtoneUri(Context context) {
        return RingtoneManager.getActualDefaultRingtoneUri(context,
                RingtoneManager.TYPE_NOTIFICATION);
    }
}
